<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位" prop="posts">
        <el-input
          v-model="queryParams.posts"
          placeholder="请输入职位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择是否启用"
          style="width: 200px"
          clearable
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="item in sysYesNo"
            :key="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['index:personnel:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['index:personnel:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['index:personnel:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="personnelList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="照片" align="center" prop="photo">
        <template v-slot="{ row }">
          <div
            class="a_b"
            v-if="row.photo && row.photo != ''"
            style="height: 40px; margin: 0 auto; width: 40px"
          >
            <img :src="baseUrl + row.photo" alt="" class="a_b_a" />
            <span class="a_b_b" style="text-align: center">
              <span
                class="a_b_b_a"
                @click="handlePictureCardPreview(row.photo)"
              >
                <el-icon><zoom-in /></el-icon>
              </span>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="职位" align="center" prop="posts" />
      <el-table-column label="简介" align="center" prop="brief" />
      <el-table-column label="是否启用" align="center" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="Y"
            inactive-value="N"
            active-text="是"
            inactive-text="否"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['index:personnel:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业人员对话框 -->
    <el-drawer :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="personnelRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="照片" prop="photo">
          <div class="a">
            <div class="a_b" v-if="form.photo && form.photo != ''">
              <img :src="baseUrl + form.photo" alt="" class="a_b_a" />
              <span class="a_b_b">
                <span
                  class="a_b_b_a"
                  @click="handlePictureCardPreview(form.photo)"
                >
                  <el-icon><zoom-in /></el-icon>
                </span>
                <span
                  class="a_b_b_b"
                  @click="handleDeletePicture(form.photo, 1)"
                >
                  <el-icon>
                    <Delete />
                  </el-icon>
                </span>
              </span>
            </div>
            <div class="a_a" @click="uploadClick(1)" v-else>
              <el-icon>
                <Plus />
              </el-icon>
              <input
                ref="inputRef1"
                name="file"
                accept=".jpg,.jpeg,.png"
                type="file"
                style="display: none"
                @change="(event) => changeFile(event, 1)"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="职位" prop="posts">
          <el-input v-model="form.posts" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="简介" prop="brief">
          <el-input
            v-model="form.brief"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
    <el-dialog v-model="dialogVisible">
      <img style="width: 100%" :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
  </div>
</template>

<script setup name="Personnel">
import {
  listPersonnel,
  getPersonnel,
  delPersonnel,
  addPersonnel,
  updatePersonnel,
} from "@/api/index/personnel";
import { getDicts } from "@/api/system/dict/data";
import { uploadFile } from "@/api/imgupload/index";
const { proxy } = getCurrentInstance();

const personnelList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    introduceId: null,
    name: null,
    posts: null,
    photo: null,
    brief: null,
    status: null,
    valiFlag: null,
  },
  rules: {
    introduceId: [
      { required: true, message: "$comment不能为空", trigger: "blur" },
    ],
    name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    posts: [{ required: true, message: "职位不能为空", trigger: "blur" }],
    photo: [{ required: true, message: "照片不能为空", trigger: "blur" }],
    brief: [{ required: true, message: "简介不能为空", trigger: "blur" }],
    status: [
      { required: true, message: "是否启用不能为空", trigger: "change" },
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" },
    ],
    createBy: [{ required: true, message: "创建人不能为空", trigger: "blur" }],
    updateTime: [
      { required: true, message: "修改时间不能为空", trigger: "blur" },
    ],
    updateBy: [{ required: true, message: "修改人不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);
// 表格开关状态
function handleStatusChange(row) {
  let text = row.status === "Y" ? "启用" : "停用";
  proxy.$modal
    .confirm('确认要"' + text + '""' + row.id + '"吗?')
    .then(function () {
      updatePersonnel({
        id: row.id,
        status: row.status,
      });
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.status = row.status === "Y" ? "N" : "Y";
    });
}
/** 查询企业人员列表 */
function getList() {
  loading.value = true;
  listPersonnel(queryParams.value).then((response) => {
    personnelList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    introduceId: null,
    name: null,
    posts: null,
    photo: null,
    brief: null,
    status: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    valiFlag: null,
  };
  proxy.resetForm("personnelRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加企业人员";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getPersonnel(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改企业人员";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["personnelRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updatePersonnel(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPersonnel(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "index/personnel/export",
    {
      ...queryParams.value,
    },
    `personnel_${new Date().getTime()}.xlsx`
  );
}
// 启用状态（字典）
const sysYesNo = ref();
function getsysYesNoList() {
  getDicts("sys_yes_no").then((res) => {
    if (res.code == 200) {
      sysYesNo.value = res.data;
    }
  });
}
//图片上传
const inputRef1 = ref();
const dialogImageUrl = ref();
const dialogVisible = ref(false);
const baseUrl = import.meta.env.VITE_BASE_URL;
const uploadClick = (type) => {
  proxy.$refs[`inputRef${type}`].click();
};
const changeFile = async (e, type) => {
  let files = e.target.files;
  for (let i = 0; i < files.length; i++) {
    let _file = files[i];
    var file = new FormData();
    file.append("files", _file);
    let res = await uploadFile(file);
    if (res.code == 200) {
      if (type == 1) {
        form.value.photo = res.fileNames;
      }
    }
  }
};
const handlePictureCardPreview = (file) => {
  dialogImageUrl.value = baseUrl + file;
  dialogVisible.value = true;
};
const handleDeletePicture = (file, type) => {
  if (type == 1) {
    form.value.photo = "";
  }
};
getList();
getsysYesNoList();
</script>
<style scoped lang="scss">
.a {
  display: flex;
  flex-wrap: wrap;
}

.a_a {
  width: 104px;
  height: 104px;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    border-color: var(--el-color-primary);
  }
}

.a_b {
  margin: 0 8px 8px 0;
  border-radius: 6px;
  width: 104px;
  height: 104px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--el-border-color);

  .a_b_a {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .a_b_b {
    align-items: center;
    background-color: var(--el-overlay-color-lighter);
    color: #fff;
    cursor: default;
    display: inline-flex;
    font-size: 20px;
    height: 100%;
    justify-content: center;
    left: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    transition: opacity var(--el-transition-duration);
    width: 100%;

    .a_b_b_a {
      cursor: pointer;
      display: none;
    }

    .a_b_b_b {
      color: inherit;
      font-size: inherit;
      position: static;
      cursor: pointer;
      display: none;
      margin-left: 1rem;
    }
  }
}

.a_b:hover {
  .a_b_b {
    opacity: 1;
  }

  .a_b_b_a {
    display: block;
  }

  .a_b_b_b {
    display: block;
  }
}

.a_subTitle {
  font-size: 12px;
  color: red;
  line-height: 1.5;
  margin-top: 4px;
  width: 100%;
}

.b_subTitle {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
  line-height: 1.7;
  margin-top: 4px;
  width: 100%;

  p {
    margin: 0;
    padding: 0;
  }
}

.c {
  display: flex;
  align-items: center;
  margin: 4px 0;
  width: 100%;

  .c_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }

  .c_b {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}

.append ::v-deep(.el-input-group__append) {
  padding: 0 10px;
}

.d {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  width: 100%;

  .d_a {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.65);
  }
}
</style>

